<template>
  <div class="home">
    <el-container>
      <el-main>
        <h1>首页</h1>
        <span>
        <el-input
          placeholder="请输入要搜索的投票项目名称"
          v-model="input"
          clearable>
          <template slot="append">
            <el-button icon="el-icon-search" circle @click="search"></el-button>
          </template>
        </el-input>
        </span>
        <el-row :gutter="20" class="el-row" type="flex" justify="space-around">
          <el-col :span="8" v-for="item in voteProjects" :key="item.projectname" class="el-col" style="margin-bottom: 20px;">
            <el-card :key="item.projectname" class="el-card" shadow="hover" @click.native="clickCard(item.id)">
              <div slot="header" class="clearfix">
                <strong>{{ item.projectname }}</strong>
              </div>
              <div class="text item">{{ item.description }}</div>
            </el-card>
          </el-col>
        </el-row>
        <el-pagination
          layout="total, prev, pager, next"
          :page-size="6"
          :current-page.sync="currentPage"
          @current-change="handleCurrentChange"
          :total.sync="totalProjectNum">
        </el-pagination>
      </el-main>
    </el-container>
  </div>
</template>

<script>

export default {
  name: 'Home',
  data() {
    return {
      voteProjects: [],
      input: '',
      totalProjectNum: 0,
      currentPage: 1,
      searchText: '',
    }
  },
  methods: {
    loadCurrentPage() {
      const _this = this
      this.$axios.post('/user/projectpage', {
        page: this.currentPage,
        pageSize: 6,
        name: this.searchText,
      }).then((res) => {
        if (res.data['code'] != 1) {
            throw('请求失败！')
          }
          this.totalProjectNum = res.data['data']['total'];
          this.voteProjects = res.data['data']['records'];
      }).catch(function (error) {
          console.debug(error);
          _this.$notify.error({
              title: '无法加载项目',
              message: '具体错误 ' + error
          })
      })
    },
    handleCurrentChange: function(currentPage) {
      console.log("handleCurrentChange");
      this.currentPage = currentPage;
      this.loadCurrentPage();
    },
    clickCard(projectId) {
      console.log("Clicked projectId", projectId);
      this.$router.push({
        path: "./projectvote",
        query: {projectid: projectId}
      });
    },
    search() {
      this.searchText = this.input;
      console.log('Search ', this.searchText);
      this.currentPage = 1;
      this.loadCurrentPage();
    }
  },
  mounted() {
    this.loadCurrentPage();
  }
}
</script>
<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .el-row {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap
  }
  .el-card {
    height: 100%;
  }
  .el-card.is-hover-shadow {
    background-color: #FAFAFA;
  }
</style>